! Convert to #coord number to DMS
! Results return in dms list and return value formatter for GeoHack
Sub ConvertToDMS #coord,#dir
  if #coord<0
    #cabs=-#coord
    #sign=-1
  else
    #cabs=#coord
    #sign=1
  endif
  #degrees=int(#cabs)
  #minutesd=60*(#cabs-#degrees)
  #minutes=int(#minutesd)
  #seconds=60*(#minutesd-#minutes)
  
  ! create a list (caller could read it)
  CreateList dms,#sign*#degrees,#minutes,#seconds
  
  ! create a string for Geo_Hack
  #secs=round(#seconds&" 2")
  #ghdms=#degrees&"_"&#minutes&"_"&#secs
  if #dir=0
    if #sign>0
      #ghdms&="_N"
    else
      #ghdms&="_S"
    endif
  else
    if #sign>0
      #ghdms&="_E"
    else
      #ghdms&="_W"
    endif
  endif
  return #ghdms
EndSUb

!gcapp.set "messageVisible","false"
gcapp.get gdoc,frontDocument

! decode lat lon
gdoc.get details,"editingDetails"
if @details.count<3
  UserOption "#var","Select a field the location coordinates and try again.","","OK"
  exit
endif
details.2.get bnds
LonLatBounds bounds,@bnds.contents
if @bounds.count<4
  UserOption "#var","The text in the editing field could be decoded.",@bounds.0,"OK"
  exit
endif

! convert to DMS and open URL
#lat=@bounds.1+0.5*@bounds.3
GoSub ConvertToDMS,#lat,0
#ghurl2=#returnValue$

#lon=@bounds.0+0.5*@bounds.2
GoSub ConvertToDMS,#lon,1
#ghurl3=#returnValue$

! open web site
#ghurl="http://toolserver.org/~geohack/geohack.php?params="&#ghurl2&"_"&#ghurl3
OpenURL #ghurl
